﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;

namespace ESHOP.DAO
{
   public class BuyLeadDAO:BaseDAO
    {
       public BUY Get(String ma)
       {
           return _db.BUYs.Where(p => p.ID.Equals(ma) && p.ISHOW.Equals(true)).SingleOrDefault();
       }
       public List<BUY> GetBuyCompany(String ma)
       {
           return _db.BUYs.Where(p => p.MACONGTY.Equals(ma) && p.ISHOW==true).ToList();
       }

       public int  AddNewBuyLead(BUY buy)
       {
           try
           {
               _db.BUYs.InsertOnSubmit(buy);
               _db.SubmitChanges();
               return 1;
           }
           catch
           {
               return 0;
           }
            
       }

       public List<BUY> SearchBuy(String id,String Keyword, String danhmuc, String xuatxu)
       {
           var Query = _db.BUYs.ToList();
         
               if (!String.IsNullOrEmpty(Keyword))
               {
                   Query = Query.Where(p => p.SUBJECT.Contains(Keyword) || p.MOTA.Contains(Keyword)).ToList();
               }
               if (!String.IsNullOrEmpty(danhmuc) && !danhmuc.Contains("Tất cả"))
               {
                   Query = Query.Where(P => P.SUBJECT.Contains(danhmuc)).ToList();
               }
               if (!String.IsNullOrEmpty(xuatxu))
               {
                   Query = Query.Where(p => p.MAQUOCGIA.Equals(xuatxu)).ToList();
               }
           
          
           Query = Query.Where(p => p.ISHOW.Equals(true)).ToList();
           return Query.OrderByDescending(p => p.ID).ToList();

       }
       public List<BUY>  GetAll()
       { 
           return _db.BUYs.Where(p=>p.ISHOW.Equals(true)).ToList();
       }
       public List<BUY> GetFromCategory(String madm)
       {
           return _db.BUYs.Where(p => p.MADM.Equals(madm)&&p.ISHOW.Equals(true)).ToList();
       }
      
       public int Delele(string id)
       {
           
           try 
           {
               var buyObject = Get(id);
               if (buyObject != null)
               {
                   buyObject.ISHOW = null;
                   _db.SubmitChanges();
               }
               return 1;
           }
           catch
           {
               return 0;
           }
       }

       public List<BUY> Search(String Name)
       {
           return _db.BUYs.Where(p => p.SUBJECT.Contains(Name)).OrderBy(p => p.NGAYDANG).ToList();
       }

       public int ActiveProduct(BUY obj)
       {
           var sanpham = Get(obj.ID);
           if (sanpham != null)
           {
               sanpham.ISHOW = true;
               _db.SubmitChanges();
               return 1;
           }
           return 0;
       }
       public int DeActiveProduct(BUY obj)
       {
           var sanpham = Get(obj.ID);
           if (sanpham != null)
           {
               sanpham.ISHOW = false;
               _db.SubmitChanges();
               return 1;
           }
           return 0;
       }

    }

}
